/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var middleNode = function(head) {
  let length = 0, temp = head;
  while(temp) {
    temp = temp.next;
    length++;
  }
  temp = head;
  let mid;
  if(length % 2) {
    mid = Math.floor(length / 2);
  } else {
    mid = length / 2;
  }
  while(mid) {
    temp = temp.next;
    mid--;
  }
  return temp;
};
